<?php

/**
 * userPlant actions.
 *
 * @package    seth
 * @subpackage userPlant
 * @author     Fuz
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userPlantActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    //$assocs=$this->getRoute()->getObjects();
    
    if ($request->isXmlHttpRequest()){     
        if ( $request->hasParameter('userPlant_user') and ($request->getParameter('userPlant_user')<>"") )
            $this->getUser()->setAttribute("selectedUser", $request->getParameter('userPlant_user',false));
        else
            $this->getUser()->setAttribute("selectedUser", FALSE); 
    }
    
    $this->form=new UserPlantForm(null, array("user"=>$this->getUser()));
    
    if ($request->isMethod("put"))
        $this->processForm($request, $this->form);
    
    if ($request->isXmlHttpRequest()){    
        return $this->renderPartial("userPlant/userPlantForm", array("form"=>$this->form));
         
    }
  }
  
  public function processForm(sfWebRequest $request, UserPlantForm $form)
  {
      
      $this->forward404Unless($request->isMethod('put'));
      $this->form->bind($request->getParameter('userPlant'));
      $user=Doctrine_Core::getTable("sfGuardUser")->find($this->form->getValue("user"));
      
      if ($this->form->isValid()){
          $result=Doctrine_Core::getTable("AssocUserPlant")->assocPlants($user,$this->form->getValue("assocPlant"));  
      
          if ($result === TRUE){
              $this->redirect('userPlant/index');
          }else{
              $this->errors=$result;
          }
          
      }
   }
}